import flash.events.Event;
import flash.events.FocusEvent;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.events.KeyboardEvent;
import flash.geom.Matrix;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFormat;

import CustomSprite;

import Images;
import Button;
class PopUpTextEvent extends Event{
public var Number:String;
	public function new(customEventString:String,inNumber:String){
        Number = inNumber;
	super(customEventString, true, false);

    }
}

class PopUpText extends CustomSprite
{
private var OKButton:Sprite;
public var TextBackground:Sprite;
public var text:TextField;
public var input:TextField;
var mKeyDown:Array<Bool>;
var allowKeyPressOnStart:Bool;

  public function new(inBackground:BitmapData,Message:String,?BackgroundColor:Int=0xFFFFFF,?FrontColor:Int=0x000000)
  {
  super();
  var Background = new CustomSprite();
  Background.graphics.beginFill(BackgroundColor);
  Background.graphics.lineStyle(1,FrontColor);
  Background.graphics.drawRect(0, 0, Constants.flashWidth, Constants.flashHeight+6);
  Background.graphics.endFill();
  Background.alpha = 0.5;
  addChild(Background);

  mKeyDown = [];
  allowKeyPressOnStart = false;
  TextBackground = new CustomSprite();
  TextBackground.graphics.beginFill(BackgroundColor);
  TextBackground.graphics.lineStyle(1,FrontColor);
  TextBackground.graphics.drawRect(0, 0, 300, 200);
//  TextBackground.(350,200);
  TextBackground.x = 150;TextBackground.y = 150;
  TextBackground.graphics.endFill();
  addChild(TextBackground);

  text = new TextField();
  text.htmlText = Message;
  addChild(text);
  text.x = 200; text.y= 200;
  text.width = 350; text.height=50;

  input = new TextField();

  input.type = TextFieldType.INPUT;
  TextBackground.addChild(input);
  input.x = 50; input.y= 90;
  input.width = 125; input.height=50;
  input.border = true;
  input.borderColor = FrontColor;
  input.maxChars = 22;
  //var myFormat:TextFormat = new TextFormat();  
  input.textColor = FrontColor;   
  //input.setTextFormat(myFormat);
  input.addEventListener(FocusEvent.FOCUS_OUT,inputOut);

  OKButton = new Button(75,50,"OK",BackgroundColor,FrontColor);
  OKButton.x = 175;
  OKButton.y = 90;
  OKButton.buttonMode = true;
  OKButton.addEventListener(MouseEvent.CLICK, onOKButtonClick);
  TextBackground.addChild(OKButton);
  this.addEventListener(Event.ADDED_TO_STAGE,onADD);
  }
private function inputOut(event:FocusEvent) {
    try
    {
      var amount = StringTools.replace(input.text,"$","");
      amount = StringTools.replace(amount,",","");
      amount = amount.substr(0,9);
      amount = Std.string(Std.parseInt(amount));
      if(amount == null || amount == "" || amount == "null")
      {
	amount = "0";
      }
     
      input.text =  HelperMethods.format(Std.parseInt(amount),0);
    }
    catch(e:Dynamic)
    {
      input.text = "0";
    }
}
public function onADD (event:Event)
{
  //trace("Added");
  stage.focus = stage;
  //input.text = "0";
  allowKeyPressOnStart = false;
  stage.addEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown );
  stage.addEventListener(KeyboardEvent.KEY_UP, OnKeyUp );

}

public function OnKeyUp (event:KeyboardEvent)
    {
    if(!allowKeyPressOnStart)
    {
      stage.focus = input;
      allowKeyPressOnStart = true;
    }
      mKeyDown[event.keyCode] = false;
    }
public function OnKeyDown(event:KeyboardEvent)
{
      if (!mKeyDown[event.keyCode])
      {
         mKeyDown[event.keyCode] = true;
	  if(mKeyDown[13] == true)
	  {
	    Back();
	  }
      }
      
}
public function Load()
{
  //flash.Lib.current.addChild(this);
}
private function onOKButtonClick(event:MouseEvent) {
    Back();
}
  public function Back()
  {
  mKeyDown = [];
  stage.removeEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown );
  stage.removeEventListener(KeyboardEvent.KEY_UP, OnKeyUp );
      var amount = StringTools.replace(input.text,"$","");
      amount = StringTools.replace(amount,",","");
      amount = Std.string(Std.parseInt(amount));
  dispatchEvent(new PopUpTextEvent("PopUpTextEvent",amount));
  //flash.Lib.current.removeChild(this);
  }



}